/*
 * Copyright (c) 2018-2999 北京小象智慧信息科技有限公司 All rights reserved.
 *
 * https://www.xiaoxiangai.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.sys.model.SysUser;
import com.yami.shop.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.google.common.collect.Maps;
import com.yami.shop.bean.app.param.SendSmsParam;
import com.yami.shop.bean.enums.SmsType;
import com.yami.shop.service.SmsLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

@RestController
@RequestMapping("/p/sms")
@Api(tags="发送验证码接口")
public class SmsController {

    @Autowired
    private SmsLogService smsLogService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 发送验证码接口
     */
    @GetMapping("/send")
    @ApiOperation(value="发送验证码", notes="用户的发送验证码")
    public ResponseEntity<Void> audit(String mobile) {
        smsLogService.sendSms(SmsType.REGISTRATIONCODE, mobile,Maps.newHashMap());
        return ResponseEntity.ok().build();
    }

    /**
     * 验证邀请码是否正确
     *
     */
    @GetMapping("/validate")
    @ApiOperation(value="发送邀请码",notes="用户的邀请码")
    public ResponseEntity<String>  validate(String mobile,String code){
        String flag = smsLogService.checkValidCode(mobile,code,SmsType.REGISTRATIONCODE);
        return  ResponseEntity.ok(flag);
    }
    /**
     * 找回密码发送验证码接口
     */
    @GetMapping("/retrievePassword")
    @ApiOperation(value="发送验证码", notes="用户的发送验证码")
    public ResponseEntity<Void> retrievePassword(String mobile) {
        smsLogService.sendRetrievePassword(SmsType.RETRIEVECODE, mobile,Maps.newHashMap());
        return ResponseEntity.ok().build();
    }

    /**
     * 找回密码时验证邀请码是否正确
     *
     */
    @GetMapping("/checkRetrieve")
    @ApiOperation(value="发送邀请码",notes="用户的邀请码")
    public ResponseEntity<String>  sendSmsToRetrieve(String mobile,String code){
        String flag = smsLogService.sendSmsToRetrieve(mobile,code,SmsType.RETRIEVECODE);
        return  ResponseEntity.ok(flag);
    }

    /**
     * 测试发送短信给商家
     */
    @GetMapping("/sendSmsToShop")
    public ResponseEntity<Void> sendSmsToShop(){
        smsLogService.sendSmsToShop("慕莎蛋糕","17858405300","111","18721961968");
        return ResponseEntity.ok().build();
    }

    /**
     * 登陆发送验证码
     */
    @PostMapping("/loginbySendSms")
    public ResponseEntity<Boolean> loginbySendSms(String mobile){
        List<SysUser> list = sysUserService.selectByMobile(mobile);
        if(list.size()>0) {
            smsLogService.loginBySendSms(mobile);
            return ResponseEntity.ok(true);
        }
        return ResponseEntity.ok(false);
    }

}
